package leftMain;
//package org.dlsu.mt.parser;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.JTextField;

public class EditEntryGUI extends JFrame implements MouseListener{
	
	private int transferWordID;
	private int translationID;
	private String lang;
	private Vector vTranslations;
	private int counter;
	
	private JLabel labelWordSearch;
	private JLabel labelPOSSearch;
	private JLabel labelLanguageSearch;
	
	private JLabel labelWord;
	private JLabel labelPOS;
	private JLabel labelSemantics;
	private JLabel labelFschemata;
	private JLabel labelLFG;
	private JLabel labelLanguage;
	private JLabel labelTranslation;
	private JLabel labelTranslation2;
	private JLabel labelPOS2;
	private JLabel labelInformation;
	
	private JLabel labelRequiredFields;
		
	private JComboBox comboLanguageSearch;
	
	private JSeparator level1;
	private JSeparator level2;
		
	private JTextField fieldWordSearch;
	private JTextField fieldPOSSearch;
	private JTextField fieldWord;
	private JTextField fieldPOS;
	private JTextField fieldLanguage;
	private JTextField fieldSemantics;
	private JTextField fieldFschemata;
	private JTextField fieldLFG;
	
	private JTextField translationWord;
	private JTextField translationPOS;
	
	private JButton buttonSave;
	private JButton buttonCancel;
	private JButton buttonSearch;
	private JButton buttonNextWord;
	private JButton buttonPrevWord;
	private JButton buttonNextTranslation;
	private JButton buttonPrevTranslation;
	private JButton buttonAddTranslation;
	
	private boolean modified = false;	
	private LEFTTranslator left;
	
		
	public EditEntryGUI(int x, int y, LEFTTranslator left){
		
		this.left = left;
		
		//Font italics = new java.awt.Font("Microsoft Sans Serif", 2, 16);
		//Font regular = new java.awt.Font("Microsoft Sans Serif", 1, 16)
		
		setTitle("Edit Entry");
		setResizable(false);
		setAlwaysOnTop(true);
		setBounds((x+100), (y+100), 510, 475+30);
		getContentPane().setLayout(null);		
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				if(modified)
					refresh();
				dispose();
				//System.exit(0);
			}
		});		
		
		labelWord = new JLabel("Word:*");
		labelPOS = new JLabel("Part of Speech:*");
		labelSemantics = new JLabel("Semantics:");
		labelFschemata = new JLabel("Fschemata:");
		labelLFG = new JLabel("LFG:");
		labelLanguage = new JLabel("Language:");
		labelTranslation = new JLabel("Translation");
		labelInformation = new JLabel("Word Information");
		labelPOS2 = new JLabel("Part of Speech:");
		labelTranslation2 = new JLabel("Translation:");
		labelWordSearch = new JLabel("Word");
		labelPOSSearch = new JLabel("Part of Speech");
		labelLanguageSearch = new JLabel("Language");	
		labelRequiredFields = new JLabel("Fields with (*) are required fields");
		
		comboLanguageSearch = new JComboBox();
		
		level1 = new JSeparator();
		level2 = new JSeparator();;
			
		fieldWordSearch = new JTextField();
		fieldPOSSearch = new JTextField();
		fieldWord = new JTextField();
		fieldPOS = new JTextField();
		fieldLanguage = new JTextField();
		fieldSemantics = new JTextField();
		fieldFschemata = new JTextField();
		fieldLFG = new JTextField();
		translationPOS = new JTextField();
		translationWord = new JTextField();
		
		buttonSave = new JButton("Save");
		buttonCancel = new JButton("Cancel");
		buttonSearch = new JButton("Search");
		buttonNextWord = new JButton(">>>");
		buttonPrevWord = new JButton("<<<");
		buttonNextTranslation = new JButton(">>");
		buttonPrevTranslation = new JButton("<<");
		buttonAddTranslation = new JButton("Add");
		
		fieldWordSearch.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 17));
		fieldWordSearch.setBounds(15, 5, 130, 30);
		fieldWordSearch.setToolTipText("Type a word to look up. . .");
		getContentPane().add(fieldWordSearch);
		
		fieldPOSSearch.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 17));
		fieldPOSSearch.setBounds(150, 5, 70, 30);		
		getContentPane().add(fieldPOSSearch);
		
		comboLanguageSearch.setFont(new java.awt.Font("Microsoft Sans Serif", 2, 17));
		comboLanguageSearch.setBounds(225, 5, 150, 30);
		comboLanguageSearch.addItem("Please choose language. . .");
		comboLanguageSearch.addItem("English");
		comboLanguageSearch.addItem("Filipino");
		getContentPane().add(comboLanguageSearch);
		
		labelWordSearch.setFont(new java.awt.Font("Microsoft Sans Serif", 2, 10));
        labelWordSearch.setBounds(65, 30, 160, 25);
		getContentPane().add(labelPOSSearch);
		
		labelPOSSearch.setFont(new java.awt.Font("Microsoft Sans Serif", 2, 10));
        labelPOSSearch.setBounds(150, 30, 160, 25);
		getContentPane().add(labelWordSearch);	
		
		labelLanguageSearch.setFont(new java.awt.Font("Microsoft Sans Serif", 2, 10));
        labelLanguageSearch.setBounds(270, 30, 160, 25);
		getContentPane().add(labelLanguageSearch);	
		
		buttonSearch.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 17));
        buttonSearch.setToolTipText("Search");
		buttonSearch.setBounds(390, 5, 100, 30);
		buttonSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	
           		buttonSearchActionPerformed(evt);          		               
            }
        });
        
		getContentPane().add(buttonSearch);
				
		level1.setBounds(15, 50, 475, 100);		
		getContentPane().add(level1);
		
		labelRequiredFields.setFont(new java.awt.Font("Microsoft Sans Serif", 2, 12));
        labelRequiredFields.setBounds(200, 52, 200, 25);
		getContentPane().add(labelRequiredFields);
		
		labelInformation.setFont(new java.awt.Font("Microsoft Sans Serif", 2, 12));
        labelInformation.setBounds(15, 52, 160, 25);
		getContentPane().add(labelInformation);
			
		labelWord.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
        labelWord.setBounds(15, 85, 160, 25);
		getContentPane().add(labelWord);
		
		fieldWord.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
		fieldWord.setBounds(165, 85, 200, 25);
		getContentPane().add(fieldWord);
		
		labelPOS.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
        labelPOS.setBounds(15, 115, 160, 25);
		getContentPane().add(labelPOS);
		
		fieldPOS.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
		fieldPOS.setBounds(165, 115, 200, 25);
		getContentPane().add(fieldPOS);
		
		
		labelLanguage.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
        labelLanguage.setBounds(15, 145, 160, 25);
		getContentPane().add(labelLanguage);
		
		fieldLanguage.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
		fieldLanguage.setBounds(165, 145, 200, 25);
		fieldLanguage.setEditable(false);
		getContentPane().add(fieldLanguage);
		
		
		labelSemantics.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
        labelSemantics.setBounds(15, 175, 160, 25);
		getContentPane().add(labelSemantics);
		
		fieldSemantics.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
		fieldSemantics.setBounds(165, 175, 200, 25);
		getContentPane().add(fieldSemantics);		
	
		labelFschemata.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
        labelFschemata.setBounds(15, 205, 160, 25);
		getContentPane().add(labelFschemata);
		
		fieldFschemata.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
		fieldFschemata.setBounds(165, 205, 200, 25);
		getContentPane().add(fieldFschemata);	
	
		labelLFG.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
        labelLFG.setBounds(15, 205+30, 160, 25);
		getContentPane().add(labelLFG);
		
		fieldLFG.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
		fieldLFG.setBounds(165, 205+30, 200, 25);
		getContentPane().add(fieldLFG);
			
		buttonPrevWord.setFont(new java.awt.Font("Microsoft Sans Serif", 3, 14));
        buttonPrevWord.setToolTipText("Previous Word");
		buttonPrevWord.setBounds(135, 250+30, 62, 25);
		buttonPrevWord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonNextWordActionPerformed(-1);
            }
        });
        getContentPane().add(buttonPrevWord);	
        
		buttonNextWord.setFont(new java.awt.Font("Microsoft Sans Serif", 3, 14));
        buttonNextWord.setToolTipText("Next Word");
		buttonNextWord.setBounds(202, 250+30, 62, 25);
		buttonNextWord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonNextWordActionPerformed(1);
                
            }
        });
        getContentPane().add(buttonNextWord);
        
        level2.setBounds(15, 300+30, 475, 100);		
		getContentPane().add(level2);       
        
        labelTranslation.setFont(new java.awt.Font("Microsoft Sans Serif", 2, 12));
        labelTranslation.setBounds(15, 302+30, 160, 25);
		getContentPane().add(labelTranslation);		
		
		labelTranslation2.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
        labelTranslation2.setBounds(15, 335+30, 160, 20);
		getContentPane().add(labelTranslation2);
		
		labelPOS2.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
        labelPOS2.setBounds(15, 360+30, 160, 20);
		getContentPane().add(labelPOS2);
			
		translationWord.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		translationWord.setBounds(130, 335+30, 160, 20);
		getContentPane().add(translationWord);
		
		translationPOS.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		translationPOS.setBounds(130, 360+30, 160, 20);
		getContentPane().add(translationPOS);				
		
		buttonPrevTranslation.setFont(new java.awt.Font("Microsoft Sans Serif", 3, 12));
        buttonPrevTranslation.setToolTipText("Previous Translation");
		buttonPrevTranslation.setBounds(155, 400+30, 50, 20);
		buttonPrevTranslation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                //enable();
                buttonNextTranslationActionPerformed(-1);
            }
        });
        getContentPane().add(buttonPrevTranslation);
        
		buttonNextTranslation.setFont(new java.awt.Font("Microsoft Sans Serif", 3, 12));
        buttonNextTranslation.setToolTipText("Next Translation");
		buttonNextTranslation.setBounds(210, 400+30, 50, 20);
		buttonNextTranslation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                //enable();
                buttonNextTranslationActionPerformed(1);
            }
        });
        getContentPane().add(buttonNextTranslation);
        
        buttonAddTranslation.setFont(new java.awt.Font("Microsoft Sans Serif", 3, 12));
        buttonAddTranslation.setToolTipText("Add Translation");
		buttonAddTranslation.setBounds(30, 400+30, 100, 20);
		buttonAddTranslation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAddTranslationActionPerformed(evt);
            }
        });
        getContentPane().add(buttonAddTranslation);
        
        buttonSave.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 17));
        buttonSave.setToolTipText("Save");
		buttonSave.setBounds(390, 345+30, 100, 30);
		buttonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	buttonSaveWordActionPerformed(evt);
            }
        });
        getContentPane().add(buttonSave);
        
        buttonCancel.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 17));
        buttonCancel.setToolTipText("Exit");
		buttonCancel.setBounds(390, 385+30, 100, 30);
		buttonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	if(modified)
            		refresh();
				dispose();
            }
        });
        getContentPane().add(buttonCancel);
                
		disable();
		
		setVisible(true);
		
	}
	
	
	public void mouseClicked(MouseEvent e)
	{
		
	}
	public void mouseEntered(MouseEvent e)
	{
		
	}
	public void mouseExited(MouseEvent e)
	{
		
	}
	public void mousePressed(MouseEvent e)
	{
		
	}
	public void mouseReleased(MouseEvent e)
	{
		
	}
	
	public void disable(){
		
		labelWord.enable(false);
		labelPOS.enable(false);
		labelSemantics.enable(false);
		labelFschemata.enable(false);
		labelLFG.enable(false);
		labelLanguage.enable(false);
		labelTranslation2.enable(false);
		labelPOS2.enable(false);
												
		fieldWord.enable(false);
		fieldPOS.enable(false);
		fieldLanguage.enable(false);
		fieldSemantics.enable(false);
		fieldFschemata.enable(false);
		fieldLFG.enable(false);
		translationWord.enable(false);
		translationPOS.enable(false);
		
		//buttonCancel.enable(false);
		buttonSave.setEnabled(false);
		
		buttonNextWord.setEnabled(false);
		buttonPrevWord.setEnabled(false);
		
		buttonNextTranslation.setEnabled(false);
		buttonPrevTranslation.setEnabled(false);
		
		buttonAddTranslation.setEnabled(false);
		
		repaint();		
	}
	
	public void clear(){
		
		fieldWord.setText("");
		fieldPOS.setText("");
		fieldLanguage.setText("");
		fieldSemantics.setText("");
		fieldFschemata.setText("");
		fieldLFG.setText("");
		translationWord.setText("");
		translationPOS.setText("");
		fieldWordSearch.setText("");
		fieldPOSSearch.setText("");
		comboLanguageSearch.setSelectedIndex(0);
		
		fieldWordSearch.requestFocus();
	}
	public void enable(){
		
		labelWord.enable(true);
		labelPOS.enable(true);
		labelSemantics.enable(true);
		labelFschemata.enable(true);
		labelLFG.enable(true);
		labelLanguage.enable(true);
		labelTranslation2.enable(true);
		labelPOS2.enable(true);
										
		fieldWord.enable(true);
		fieldPOS.enable(true);
		fieldLanguage.enable(true);
		fieldSemantics.enable(true);
		fieldFschemata.enable(true);
		fieldLFG.enable(true);
		translationWord.enable(true);
		translationPOS.enable(true);
		
		buttonSave.setEnabled(true);
		
		//buttonCancel.enable(true);
		
		buttonNextWord.setEnabled(true);
		buttonPrevWord.setEnabled(true);
		
		buttonNextTranslation.setEnabled(true);
		buttonPrevTranslation.setEnabled(true);
		
		buttonAddTranslation.setEnabled(true);
		repaint();
	}
	
	private void buttonSaveWordActionPerformed(java.awt.event.ActionEvent evt){
		
		//javax.swing.JOptionPane.showMessageDialog(this, "Save.", "Edit Entry", 1);
			
		try{
   			
   			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        	
	        Connection con = DriverManager.getConnection("jdbc:odbc:leftDB", "",""); 
			
   			Statement stmt = con.createStatement();	
   			
   			//javax.swing.JOptionPane.showMessageDialog(this, "abot dito", "Edit Entry", javax.swing.JOptionPane.OK_OPTION);
   			
   			String word = fieldWord.getText();
   			String POS = fieldPOS.getText();
   			String sem = fieldSemantics.getText();
   			String fs = fieldFschemata.getText();
   			String lfg = fieldLFG.getText();
   			String tWord = translationWord.getText();
   			String tPOS = translationPOS.getText();
   			int tempID;
   			
   			if(word.equals("") || POS.equals("")){
   				javax.swing.JOptionPane.showMessageDialog(this, "Required field(s) missing.", "Edit Entry", javax.swing.JOptionPane.OK_OPTION);
   				return;	
   			}   			
   			
   			try{
	   			
	   			if(lang.equals("1")){
	   				
	   				stmt.executeUpdate("UPDATE language1Dict SET word = '"+word+"', POS = '"+POS+"', semantics = '"+sem+"', fschemata = '"+fs+"', lfg = '"+lfg+"' Where id = "+transferWordID+" ");
	   				
	   			}else{
	   				
	   				stmt.executeUpdate("UPDATE language2Dict SET word = '"+word+"', POS = '"+POS+"', semantics = '"+sem+"', fschemata = '"+fs+"', lfg = '"+lfg+"' Where id = "+transferWordID+" ");
	   			}	   			
	   			
	   				   			
	   			
	   		}catch(Exception err){
	   			javax.swing.JOptionPane.showMessageDialog(this, err.getMessage(), "Edit Entry", javax.swing.JOptionPane.OK_OPTION);
	   		}
	   		
	   		try{
	   			ResultSet rs;
	   			
	   			if(lang.equals("1")){
	   				
	   				rs = stmt.executeQuery("SELECT id FROM language2Dict WHERE word = '"+tWord+"' AND pos = '"+tPOS+"' ");
	   				
	   			}else{
	   				
	   				rs = stmt.executeQuery("SELECT id FROM language1Dict WHERE word = '"+tWord+"' AND pos = '"+tPOS+"' ");
	   			}
	   			
	   			try{
	   				
	   				rs.next();
	   				tempID = rs.getInt("id");
	   				rs.close();
	   				stmt.executeUpdate("UPDATE transferDict SET word = '"+word+"', pos = '"+POS+"', translation = "+tempID+" WHERE transferWordID = "+transferWordID+" AND lang = '"+lang+"' ");
	   				
	   			}catch(Exception err3){
	   				//javax.swing.JOptionPane.showMessageDialog(this, "ResultSet empty.", "Edit Entry", 1);
	   				//javax.swing.JOptionPane.showMessageDialog(this, err3.getMessage(), "Edit Entry", 1);
	   				//tempID = translationID;
	   				stmt.executeUpdate("UPDATE transferDict SET word = '"+word+"', pos = '"+POS+"' WHERE transferWordID = "+transferWordID+" AND lang = '"+lang+"' "); 				
	   			}
	   			
	   				   			
	   		}catch(Exception err2){
	   			
	   		}	   		
	   		
	   		con.close();
	   		stmt.close();
	   		clear();
	   		disable();
	   		modified = true;
	   		javax.swing.JOptionPane.showMessageDialog(this, "Update successful.", "Edit Entry", 1);
	   		
		}catch(Exception e){
			
			javax.swing.JOptionPane.showMessageDialog(this, e.getMessage(), "Edit Entry", javax.swing.JOptionPane.OK_OPTION);
			
		}	
	}
	
	private void buttonAddTranslationActionPerformed(java.awt.event.ActionEvent evt){
		
		String query = "";
		String word = fieldWord.getText();
   		String POS = fieldPOS.getText();
   		String tWord = translationWord.getText();
   		String tPOS = translationPOS.getText();
		int tempID;
		
		try{
			
			if(tWord.equals("") || tPOS.equals("")){
				javax.swing.JOptionPane.showMessageDialog(this, "Please complete translation data.", "Edit Entry", javax.swing.JOptionPane.OK_OPTION);
				return;
			}
			
			
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        	
	    	Connection con = DriverManager.getConnection("jdbc:odbc:leftDB", "",""); 
			
   			Statement stmt = con.createStatement();   			
   			
   			if(lang.equals("1"))
   				query = "SELECT * FROM language2Dict WHERE word = '"+tWord+"' AND pos = '"+tPOS+"' ";
   			else
   				query = "SELECT * FROM language1Dict WHERE word = '"+tWord+"' AND pos = '"+tPOS+"' ";
   			
   			
   			ResultSet rs = stmt.executeQuery(query);
   			
   			try{
   				
   				rs.next();
   				tempID = rs.getInt("id");
   				rs.close();
   				
   			}catch(Exception err){
   				javax.swing.JOptionPane.showMessageDialog(this, "Word not in the dictionary.", "Edit Entry", javax.swing.JOptionPane.OK_OPTION);
   				return;
   			}
   			
   			if(checkForDuplicate(tempID)){
   				javax.swing.JOptionPane.showMessageDialog(this, "Translation already exists in the dictionary.", "Edit Entry", javax.swing.JOptionPane.OK_OPTION);
   			}
   			else{
   			
   				stmt.executeUpdate("INSERT INTO transferDict (transferWordID, word, pos, lang, translation) Values ("+transferWordID+", '"+word+"', '"+POS+"', '"+lang+"', "+tempID+")");
   				modified = true;
   				javax.swing.JOptionPane.showMessageDialog(this, "New translation successfully added.", "Edit Entry", 1);
   			}
   			
   			getTranslations(transferWordID);
   			
   			//stmt.executeUpdate();
   			   			
   			con.close();
   			stmt.close();   			
   			
   		}catch(Exception e){
   			
   		}
   				
	}
	
	private void buttonSearchActionPerformed(java.awt.event.ActionEvent evt){
				
		String query = "";
    	String word = fieldWordSearch.getText();
    	String POS = fieldPOSSearch.getText();
    	
    	if(!word.equals("")){
    		
    		if(!POS.equals("")){
    		
    			if(comboLanguageSearch.getSelectedIndex() == 0){
    				javax.swing.JOptionPane.showMessageDialog(this, "Choose language.", "Edit Entry", javax.swing.JOptionPane.OK_OPTION);
    				return;
    			}    		
    		}
    		else{
   				javax.swing.JOptionPane.showMessageDialog(this, "Type a part of speech.", "Edit Entry", javax.swing.JOptionPane.OK_OPTION);
   				return;
   			}
    	}
    	else{
   			javax.swing.JOptionPane.showMessageDialog(this, "Type a word.", "Edit Entry", javax.swing.JOptionPane.OK_OPTION);
   			return;
   		}
   		
   		try{
   			
   			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        	
	        Connection con = DriverManager.getConnection("jdbc:odbc:leftDB", "",""); 
			
   			Statement stmt = con.createStatement();
   			   			
   			
   			if(comboLanguageSearch.getSelectedIndex() == 1){   			
   				query = "SELECT * FROM language1Dict WHERE word = '"+word+"' AND pos = '"+POS+"' ";
   				lang = "1";
   			}
   			else{
   				query = "SELECT * FROM language2Dict WHERE word = '"+word+"' AND pos = '"+POS+"' ";
   				lang = "2";
   			}
   			
   			
   			//javax.swing.JOptionPane.showMessageDialog(this, "before", "Edit Entry", javax.swing.JOptionPane.OK_OPTION);
   			
   			ResultSet rs = stmt.executeQuery(query);
   			
   			//javax.swing.JOptionPane.showMessageDialog(this, "after", "Edit Entry", javax.swing.JOptionPane.OK_OPTION);
   			try{
	   			
	   			rs.next();
				
		   		fieldWord.setText(rs.getString("word"));
		   		fieldPOS.setText(rs.getString("POS"));
		   			
		   		if(lang.equals("1"))
		   			fieldLanguage.setText("English");
		   		else
		   			fieldLanguage.setText("Filipino");
		   				
		   		fieldSemantics.setText(rs.getString("semantics"));
		   		fieldFschemata.setText(rs.getString("fschemata"));
		   		fieldLFG.setText(rs.getString("lfg"));
		   		transferWordID = rs.getInt("id");
	   			
	   			//System.out.println(id);
	   			rs.close();			
	   				   			
	   			enable();
	   			
	   			stmt.close();
	   			con.close();
	   			getTranslations(transferWordID);
	   			
	   		}catch(Exception err){
	   			javax.swing.JOptionPane.showMessageDialog(this, "Word not found.", "Edit Entry", javax.swing.JOptionPane.OK_OPTION);
	   			//javax.swing.JOptionPane.showMessageDialog(this, err.getMessage(), "Edit Entry", javax.swing.JOptionPane.OK_OPTION);
	   			clear();
	   		}  			
	
   			   			
   		}catch(Exception e)		
		{
			System.out.println("Error "+e.toString());
		}	
		
	}
		
	private void buttonNextWordActionPerformed(int i){
		
		String query = "";
		
		try{
			
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        	
	    	Connection con = DriverManager.getConnection("jdbc:odbc:leftDB", "",""); 
			
   			Statement stmt = con.createStatement();  			
   			
   			String word = fieldWord.getText();
   			
   			if(i > 0){
	   			
				if(lang.equals("1"))
					query = "SELECT * FROM language1Dict where word > '"+word+"' ORDER BY word ASC";
				else
					query = "SELECT * FROM language2Dict where word > '"+word+"' ORDER BY word ASC"; 
			}
			else{
				
				if(lang.equals("1"))
					query = "SELECT * FROM language1Dict where word < '"+word+"' ORDER BY word DESC";
				else
					query = "SELECT * FROM language2Dict where word < '"+word+"' ORDER BY word DESC"; 
				
			}
			ResultSet rs = stmt.executeQuery(query);
			
			try{
	   			
	   			rs.next();
				
		   		fieldWord.setText(rs.getString("word"));
		   		fieldPOS.setText(rs.getString("POS"));		   				
		   		fieldSemantics.setText(rs.getString("semantics"));
		   		fieldFschemata.setText(rs.getString("fschemata"));
		   		fieldLFG.setText(rs.getString("lfg"));
		   		
		   		transferWordID = rs.getInt("id");
	   			
	   			//System.out.println(id);
	   			rs.close();
	   			
	   			stmt.close();
	   			con.close();
	   			getTranslations(transferWordID);
				
			}catch(Exception err){
			}
				
			//System.out.println(id + " " + counter);			
			
			con.close();
			stmt.close();		
							
		}catch(Exception e){
			System.out.println(e.getMessage());
			
		}
		
	}

	private void buttonNextTranslationActionPerformed(int i){
		 
		String query = "";
		int temp;
		temp = counter;
		try{
			
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        	
	    	Connection con = DriverManager.getConnection("jdbc:odbc:leftDB", "",""); 
			
   			Statement stmt = con.createStatement();   			
   			  			
   			if(i<0)
				counter-=1;
			else
				counter+=1;
			
			translationID = (Integer)vTranslations.get(counter); 
			
			
			if(lang.equals("1"))
				query = "SELECT * FROM language2Dict where id = "+translationID+""; 
			else
				query = "SELECT * FROM language1Dict where id = "+translationID+""; 
						
			ResultSet rs = stmt.executeQuery(query);
			
			try{
			
				rs.next();
				translationWord.setText(rs.getString("word"));
				translationPOS.setText(rs.getString("POS"));
				rs.close();
				
			}catch(Exception err){
			}
				
			//2System.out.println(id + " " + counter);			
			
			con.close();
			stmt.close();		
							
		}catch(Exception e){
			System.out.println(e.getMessage());
			counter = temp;
		}		
	}
		
	private void getTranslations(int transferWordID){
		
		try{
   			
   			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        	
	        Connection con = DriverManager.getConnection("jdbc:odbc:leftDB", "",""); 
			
   			Statement stmt = con.createStatement();
   			
   			ResultSet rs = stmt.executeQuery("SELECT translation FROM transferDict WHERE transferWordID = "+transferWordID+" AND lang = '"+lang+"'");
   			  			
   			vTranslations = new Vector();
   			
   			while(rs.next()){
   				vTranslations.addElement(rs.getInt("translation"));
   			}			
   			counter = -1;
   			
   			rs.close();
   			
   			//rs.next();
   			//System.out.println(rs.getInt("translation"));
   			
   			//translationWord.setText(rs.getInt("transferWordID") + "");
   			//translationPOS.setText("");
   			
   			con.close();
   			stmt.close();
   			translationWord.setText("");
   			translationPOS.setText("");
   			buttonNextTranslationActionPerformed(1);
   			
		}catch(Exception e){
			System.out.println(e.getMessage() + " ===> error");
		}   			
	}
	
	
	private boolean checkForDuplicate(int translation){
    	    	
    	String msg = "";
    	String query = "";
    	int counter = 0	;
   		
   		try{
   			
   			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        	
	        Connection con = DriverManager.getConnection("jdbc:odbc:leftDB", "",""); 
			
   			Statement stmt = con.createStatement();
   			
   			query = "SELECT * FROM transferDict WHERE transferWordID = "+transferWordID+" AND lang = '"+lang+"' AND translation = "+translation+"";
   			   			
			ResultSet rs = stmt.executeQuery(query);
									
			try
			{
				while(rs.next())
					counter++;
			} catch ( SQLException e ) {}
			
			stmt.close();
			con.close();
			
			if(counter == 0)
				return false;
			else
				return true;
			
						
   		}catch(Exception e)
		
		{
			System.out.println("Error "+e.toString());
			msg = e.toString();
		}
		
    	return false;    	
    }
    
    private void refresh(){
		
		left.setLanguage1Dictionary(new org.dlsu.mt.parser.dictionary.Dictionary("Language1"));
		left.setLanguage2Dictionary(new org.dlsu.mt.parser.dictionary.Dictionary("Language2"));
	}
}